<template>
  <el-dialog :model-value="visible" @update:model-value="(value) => visible = value" :title="title" width="800px" :before-close="handleClose">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="评估编号" prop="assessmentCode">
            <el-input v-model="formData.assessmentCode" placeholder="请输入评估编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="水库名称" prop="reservoirName">
            <el-select v-model="formData.reservoirName" placeholder="请选择水库" filterable>
              <el-option label="东圳水库" value="东圳水库" />
              <el-option label="金钟水库" value="金钟水库" />
              <el-option label="西音水库" value="西音水库" />
              <el-option label="古洋水库" value="古洋水库" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="评估类型" prop="assessmentType">
            <el-select v-model="formData.assessmentType" placeholder="请选择评估类型">
              <el-option label="年度评估" value="annual" />
              <el-option label="专项评估" value="special" />
              <el-option label="应急评估" value="emergency" />
              <el-option label="第三方评估" value="third-party" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="评估机构" prop="assessmentOrg">
            <el-input v-model="formData.assessmentOrg" placeholder="请输入评估机构" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="评估时间" prop="assessmentDate">
            <el-date-picker
              v-model="formData.assessmentDate"
              type="date"
              placeholder="选择评估时间"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="评估人员" prop="assessor">
            <el-input v-model="formData.assessor" placeholder="请输入评估人员" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="评估状态" prop="status">
            <el-select v-model="formData.status" placeholder="请选择状态">
              <el-option label="计划中" value="planned" />
              <el-option label="评估中" value="assessing" />
              <el-option label="已完成" value="completed" />
              <el-option label="已暂停" value="paused" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安全等级" prop="safetyLevel">
            <el-select v-model="formData.safetyLevel" placeholder="请选择安全等级">
              <el-option label="A级(优秀)" value="A" />
              <el-option label="B级(良好)" value="B" />
              <el-option label="C级(合格)" value="C" />
              <el-option label="D级(不合格)" value="D" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="评估得分" prop="score">
            <el-input-number v-model="formData.score" :min="0" :max="100" placeholder="请输入评估得分" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="评估费用" prop="cost">
            <el-input-number v-model="formData.cost" :min="0" placeholder="请输入评估费用" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="评估结果" prop="result">
        <el-input
          v-model="formData.result"
          type="textarea"
          :rows="3"
          placeholder="请输入评估结果"
        />
      </el-form-item>
      
      <el-form-item label="问题描述" prop="issueDescription">
        <el-input
          v-model="formData.issueDescription"
          type="textarea"
          :rows="3"
          placeholder="请输入发现的问题描述"
        />
      </el-form-item>
      
      <el-form-item label="改进建议" prop="suggestion">
        <el-input
          v-model="formData.suggestion"
          type="textarea"
          :rows="3"
          placeholder="请输入改进建议"
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="2"
          placeholder="请输入备注信息"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading" v-if="!isView">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch, computed } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

// 定义组件属性
interface Props {
  modelValue: boolean
  title: string
  assessmentData?: any
}

// 定义事件
interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  assessmentData: null
})

const emit = defineEmits<Emits>()

// 模拟API接口
const saveAssessment = async (data: any) => {
  // 模拟保存
  return { code: 200, msg: '保存成功' }
}

const updateAssessment = async (data: any) => {
  // 模拟更新
  return { code: 200, msg: '更新成功' }
}

// 响应式数据
const loading = ref(false)
const formRef = ref<FormInstance>()

// 表单数据
const formData = reactive({
  id: null,
  assessmentCode: '',
  reservoirName: '',
  assessmentType: '',
  assessmentOrg: '',
  assessmentDate: '',
  assessor: '',
  status: 'planned',
  safetyLevel: '',
  score: 0,
  cost: 0,
  result: '',
  issueDescription: '',
  suggestion: '',
  remark: ''
})

// 表单验证规则
const rules: FormRules = {
  assessmentCode: [
    { required: true, message: '请输入评估编号', trigger: 'blur' }
  ],
  reservoirName: [
    { required: true, message: '请选择水库名称', trigger: 'change' }
  ],
  assessmentType: [
    { required: true, message: '请选择评估类型', trigger: 'change' }
  ],
  assessmentOrg: [
    { required: true, message: '请输入评估机构', trigger: 'blur' }
  ],
  assessmentDate: [
    { required: true, message: '请选择评估时间', trigger: 'change' }
  ],
  assessor: [
    { required: true, message: '请输入评估人员', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择评估状态', trigger: 'change' }
  ],
  result: [
    { required: true, message: '请输入评估结果', trigger: 'blur' }
  ]
}

// 计算属性
const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const isView = computed(() => props.title.includes('查看'))

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    id: null,
    assessmentCode: '',
    reservoirName: '',
    assessmentType: '',
    assessmentOrg: '',
    assessmentDate: '',
    assessor: '',
    status: 'planned',
    safetyLevel: '',
    score: 0,
    cost: 0,
    result: '',
    issueDescription: '',
    suggestion: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 监听弹窗显示状态
watch(visible, (newVal) => {
  if (newVal) {
    if (props.assessmentData) {
      // 编辑或查看模式，填充数据
      Object.assign(formData, props.assessmentData)
    } else {
      // 新增模式，重置表单
      resetForm()
    }
  }
})

// 关闭弹窗
const handleClose = () => {
  visible.value = false
  resetForm()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    if (formData.id) {
      // 更新
      await updateAssessment(formData)
      ElMessage.success('更新成功')
    } else {
      // 新增
      await saveAssessment(formData)
      ElMessage.success('新增成功')
    }
    
    emit('success')
  } catch (error) {
    console.error('提交失败:', error)
    ElMessage.error('提交失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>